<?php
if (COUNT($_POST) > 0)
{
	$issearch = $this->get('searching');
	$key = $this->get('key');
	$date = trim($this->get('date'));
	$date2 = trim($this->get('date2'));
	$ispay = $this->get('paying');
	$display = $this->get('display');
	$islist = $this->get('list');
	$countoverdue = $this->get('countoverdue');
	$countdelivered = $this->get('countdelivered');
	$countunpaid = $this->get('countunpaid');
	$setdelivered = $this->get('setdelivered');
	$plist = $this->get('plist');
	$setcheck = $this->get('setcheck');
	
	Connection::getConnect();
	if ($countunpaid)
	{
		$sql = "SELECT COUNT(iscid) FROM item_sales_credit isc WHERE NOT balance=0 AND NOT iscid = 0";
		Connection::setQuery($sql);
		$count = Connection::fetch();
		echo $count[0];
		exit;
	} 
	if ($countoverdue)
	{
		$sql = "SELECT COUNT(iscid) FROM item_sales_credit isc WHERE NOT balance=0 AND DATEDIFF(DATE_ADD(isc.date, INTERVAL 30 DAY), NOW()) < 0 AND NOT iscid = 0";
		Connection::setQuery($sql);
		$count = Connection::fetch();
		echo $count[0];
		exit;
	} 
	if ($countdelivered)
	{
		$sql = "SELECT COUNT(iscid) FROM item_sales_credit isc WHERE delivered IS NULL AND NOT iscid = 0";
		Connection::setQuery($sql);
		$count = Connection::fetch();
		echo $count[0];
		exit;
	} 
	if (strlen($date2) < 10)
		$date2 = $date;
	if ($issearch)
	{
		$sql = "SELECT iscid, name, DATE_FORMAT(delivered,'%b %D, %Y - %h:%i ') AS delivered, DATE_FORMAT(isc.date,'%b %D, %Y - %h:%i ') da, orn, balance, l.address, DATE_FORMAT(DATE_ADD(isc.date, INTERVAL 30 DAY),'%M %d, %Y') AS due, DATEDIFF(DATE_ADD(isc.date, INTERVAL 30 DAY), NOW()) AS due2,(SELECT COUNT(iscid) FROM item_sales_credit_log iscl WHERE iscl.status='P' AND iscl.iscid=isc.iscid) AS pending FROM item_sales_credit isc JOIN login l USING(aid) JOIN item_sales i USING(iscid) WHERE ((name LIKE '%$key%' OR orn LIKE '%$key%') AND ((DATE_FORMAT(isc.date,'%Y-%m-%d') BETWEEN '$date' AND '$date2') OR '$date'='') AND NOT iscid = 0) GROUP BY orn ORDER BY orn";
		Connection::setQuery($sql);
		$result = Connection::fetchAll(MYSQL_ASSOC);
		foreach ($result as $item){
			if (
				(!($item['balance'] == 0 && $display == 1)
				&& !($item['balance'] != 0 && $display == 2)
				&& !($item['due2'] < 0 && $display == 3)
				&& !($item['delivered'] != '' && $display == 4)
				&& !($item['delivered'] == '' && $display == 5))
				&& $display != 0
			 	|| $item['iscid'] == 0)
				continue;
			echo '<tr class="list searches ease" iscid="' . $item['iscid'] . '" due="' . $item['due'] . '" left="' . $item['due2'] . '" date="' . $item['da'] . '" pending="' . $item['pending'] . '" delivered="' . $item['delivered'] . '" address="' . $item['address'] . '">' ;
    		echo '<td style="width:100px">' . $item['orn'] . '</td>' ;
    		echo '<td style="width:200px;" class="ui-text-center">' . $item['name'] . '</td>' ;
    		echo '<td style="width:75px;" class="ui-text-center">' . $item['balance'] . '</td>' ;
			echo '</tr>';
		}
		exit;
	}
	if ($ispay)
	{
		$cash = $this->get('cash');
		$check = $this->get('check');
		$bank = $this->get('bank');
		$balance = $this->get('balance');
		$status = 'P';
		
		if(strlen($check) < 34)
			$status = 'V';

		Connection::setQueryf("INSERT INTO item_sales_credit_log(amount, cheque, bank, iscid, status, date, aid) VALUES(%d, '%s', '%s', %d, '%s', NOW(), %d);", $cash, $check, $bank, $key, $status, $login->aid);
		if($status == 'P')
			$cash = 0;
		if($cash > $balance)
			$cash = $balance;
		Connection::setQueryf("UPDATE item_sales_credit SET balance=balance-%d WHERE iscid=%d;", $cash, $key);
		Connection::setQueryf("SELECT SUM(total_sales)*(100-discount)/100 FROM item_sales iss WHERE iscid=%d",$key);
		$count = Connection::fetch();
		echo $count[0];
		exit;
	}
	if ($islist)
	{
		$sql = "SELECT *,ids.name AS iname, iud.name AS unit, iss.quantity AS qty FROM item_sales iss JOIN item_details ids USING(iid) JOIN item_inventory USING(iid) JOIN supplier_item_cost USING(iid) JOIN item_unit_detail iud USING(imdid) WHERE iscid=$key GROUP BY iid";
		Connection::setQuery($sql);
		$result = Connection::fetchAll(MYSQL_ASSOC);
		foreach ($result as $item){
			echo '<tr class="list">';
            echo '	<td style="width: 100px;" class="ui-text-center">' . $item['barcode'] . '</td>';
            echo '	<td style="width: 250px;" class="ui-text-left">' . $item['iname'] . '</td>';
            echo '	<td style="width: 100px;" class="ui-text-center">' . $item['unit'] . '(' . $item['per_unit'] . ')</td>';
            echo '	<td style="width: 75px;" class="ui-text-center">' . $item['qty'] . '</td>';
            echo '	<td style="width: 100px;" class="ui-text-center">' . $item['whole_sale_price'] . '</td>';
            echo '	<td style="width: 100px;" class="ui-text-center">' . $item['total_sales'] . '</td>';
        	echo '</tr>';
		}
		exit;
	}
	
	if ($plist)
	{
		$sql = "SELECT isclid, (SELECT SUM(total_sales)*(100-discount)/100 FROM item_sales iss WHERE iss.iscid=$key) AS balance, interest, DATE_FORMAT(i.date,'%Y-%m-%d') as date, amount, cheque, bank, i.status FROM item_sales_credit_log i JOIN item_sales_credit isc USING(iscid) WHERE i.iscid=$key;";
		Connection::setQuery($sql);
		$result = Connection::fetchAll(MYSQL_ASSOC);
		foreach ($result as $item){
			echo '<tr class="list" balance="' . $item['balance'] . '" interest="' . $item['interest'] . '" amount="' . $item['amount'] . '" isclid="' . $item['isclid'] . '">';
			echo '	<td style="width: 80px;">' . $item['date'] . '</td>';
			echo '	<td style="width: 170px;">' . $item['amount'] . '</td>';
			echo '	<td style="width: 300px;">';
			if (strlen($item['cheque']) == 34)
			{
				echo $item['bank'] . ' <br /> ' . $item['cheque'];
			} else {
				echo 'Cash';
			}
			echo '</td>';
			echo '	<td style="width: 120px;">';
				if($item['status'] == 'I') echo "Invalid";
				if($item['status'] == 'V') echo "Successful";
			echo '	</td>';
			echo '</tr>';
		}
		exit;
	}
	if ($setdelivered)
	{
		$sql = "UPDATE item_sales_credit isc SET delivered=NOW() WHERE iscid=%d";
		Connection::setQueryf($sql, $key);
		exit;
	}
	if ($setcheck)
	{
		$status = $this->get('status');
		$sql = "UPDATE item_sales_credit_log isc SET status='%s' WHERE isclid=%d";
		Connection::setQueryf($sql, $status, $key);
		Connection::setQueryf("SELECT i.iscid, balance, interest, amount, i.status FROM item_sales_credit_log i JOIN item_sales_credit isc USING(iscid) WHERE isclid=%d;", $key);
		$array = Connection::fetch();
		if($array[4] == "V"){
			$balance = $array[1];
			$amt = $array[3];
			if($balance < $amt )
				$amt = $balance;
  			Connection::setQueryf("UPDATE item_sales_credit SET balance=balance-%d WHERE iscid=%d;", $amt, $array[0]);
		}
		exit;
	}
}
?>
